package android.demo.activity.menu;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.TextView;

import android.demo.R;

public class MenuActivity extends Activity {

	private static final int ITEM1 = Menu.FIRST;
	private static final int ITEM2 = 2;
	private static final int ITEM3 = 3;
	private static final int ITEM4 = 4;
	private static final int ITEM5 = 5;
	private static final int ITEM6 = 6;
	private static final int ITEM7 = 7;
	private static final int COLOR_ITEM_RED = 11;
	private static final int COLOR_ITEM_GREEN = 12;
	private static final int COLOR_ITEM_WHITE = 13;
	private static final int TITLE_ITEM1 = 21;
	private static final int TITLE_ITEM2 = 22;
	private static final int TITLE_ITEM3 = 23;
	private TextView tvText;
	
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.menu);
		setTitle("菜单");
		tvText = (TextView) findViewById(R.id.menu_TvText);
		registerForContextMenu(tvText);
	}
	
	
	public boolean onCreateOptionsMenu(Menu menu) {
		SubMenu color = menu.addSubMenu("颜色");
		SubMenu title = menu.addSubMenu("标题");
		menu.add(0, ITEM1, 0, "菜单1");
		menu.add(0, ITEM2, 0, "菜单2");
		menu.add(0, ITEM3, 0, "菜单3");
		menu.add(0, ITEM4, 0, "菜单4");
		menu.add(0, ITEM5, 0, "菜单5");
		color.add(0, COLOR_ITEM_RED, 0, "红色背景");
		color.add(0, COLOR_ITEM_GREEN, 0, "绿色背景");
		color.add(0, COLOR_ITEM_WHITE, 0, "白色背景");
		title.add(0, TITLE_ITEM1, 0, "子菜单标题1");
		title.add(0, TITLE_ITEM2, 0, "子菜单标题2");
		title.add(0, TITLE_ITEM3, 0, "子菜单标题3");
		return true;
	}
	
	
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case ITEM1 :
				setTitle("菜单标题1");
				break;
			case ITEM2 :
				setTitle("菜单标题2");
				break;
			case ITEM3 :
				setTitle("菜单标题3");
				break;
			case ITEM4 :
				setTitle("菜单标题4");
				break;
			case ITEM5 :
				setTitle("菜单标题5");
				break;
			case COLOR_ITEM_RED :
				tvText.setBackgroundColor(Color.RED);
				break;
			case COLOR_ITEM_GREEN :
				tvText.setBackgroundColor(Color.GREEN);
				break;
			case COLOR_ITEM_WHITE :
				tvText.setBackgroundColor(Color.WHITE);
				break;
			case TITLE_ITEM1 :
				setTitle("子菜单标题1");
				break;
			case TITLE_ITEM2 :
				setTitle("子菜单标题2");
				break;
			case TITLE_ITEM3 :
				setTitle("子菜单标题3");
				break;
		}
		return true;
	}
	
	
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		menu.add(0, ITEM1, 0, "ITEM1");
		menu.add(0, ITEM2, 0, "ITEM2");
		menu.add(0, ITEM3, 0, "ITEM3");
		menu.add(0, ITEM4, 0, "ITEM4");
		menu.add(0, ITEM5, 0, "ITEM5");
		menu.add(0, ITEM6, 0, "ITEM6");
		menu.add(0, ITEM7, 0, "ITEM7");
	}
	
	
	public boolean onContextItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case ITEM1 :
			tvText.setBackgroundColor(Color.RED);
			break;
		case ITEM2 :
			tvText.setBackgroundColor(Color.GREEN);
			break;
		case ITEM3 :
			tvText.setBackgroundColor(Color.WHITE);
			break;
		case ITEM4 :
			setTitle("ITEM4");
			break;
		case ITEM5 :
			setTitle("ITEM5");
			break;
		case ITEM6 :
			setTitle("ITEM6");
			break;
		case ITEM7 :
			setTitle("ITEM7");
			break;
	}
	return true;
	}
	
	
}
